﻿using System;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace NetSurveillance.WebApp.Exts
{
    public class JsonNetResult : JsonResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            if(context == null)
                throw new ArgumentNullException("context");
            var response = context.HttpContext.Response;

            response.ContentType = !string.IsNullOrWhiteSpace(ContentType) ? ContentType : "application/json";

            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;

            if (Data == null)
                return;

            var serializedObject = JsonConvert.SerializeObject(Data, Formatting.Indented,
                                                               new JsonSerializerSettings
                                                                   {
                                                                       ContractResolver = new CamelCasePropertyNamesContractResolver(),
                                                                       NullValueHandling = NullValueHandling.Ignore
                                                                   });
            response.Write(serializedObject);
        }
    }
}